from appium.options.android import UiAutomator2Options
from appium import webdriver
from appium.webdriver.common.appiumby import *


class CommonDriver:
    def get_driver(self):
        options = UiAutomator2Options()
        options.platform_name = 'Android'
        options.platform_version = '7'
        options.app_package = 'com.dangdang.buy2'
        options.app_activity = 'com.dangdang.buy2.activity.ActivityMainTab'
        options.no_reset = False
        options.unicode_keyboard = True
        options.udid = '127.0.0.1:21513'

        command_executor_url = "http://127.0.0.1:4723/wd/hub"

        driver = webdriver.Remote(command_executor=command_executor_url, options=options)

        driver.implicitly_wait(10)

        print("连接成功")

        return driver

if __name__ == '__main__':
    driver = CommonDriver().get_driver()
    driver.find_element(AppiumBy.XPATH,
                        '//android.widget.ViewFlipper[@resource-id="com.dangdang.buy2:id/home_view_flipper"]').click()
    driver.find_element(AppiumBy.XPATH,'//android.widget.EditText[@resource-id="com.dangdang.buy2:id/et_search"]').send_keys("浪潮之巅")

    driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/tv_search"]').click()
    #加购
    driver.find_element(AppiumBy.XPATH,'(//android.widget.TextView[@resource-id="com.dangdang.buy2:id/add_cart_tv"])[1]').click()
    #点击购物车
    driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/etv_cart"]').click()
    # #去结算
    driver.find_element(AppiumBy.XPATH,'//android.widget.TextView[@resource-id="com.dangdang.buy2:id/cart_balance_tv"]').click()
